package com.huayun.plugin.separationsurface.service;

import com.huayun.plugin.separationsurface.domain.ProjectSurface;
import com.huayun.plugin.separationsurface.vo.SeparationSurfaceParam;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SeparationSurfaceService {
    private final MongoTemplate mongoTemplate;

    /**
     * 增加分离面
     * @param param
     * @return
     */
    public ProjectSurface addSeparationSurface(SeparationSurfaceParam param) {
        // 设置最大的index用于排序
        if (param.getSurfaceIndex() == 0) {
            List<ProjectSurface> faces = getSeparationSurfaces(param.getProjectId()
                    , param.getActiveDocId(), null);
            if (!faces.isEmpty()) {
                int index = faces.stream().mapToInt(ProjectSurface::getSurfaceIndex).max().orElse(0);
                param.setSurfaceIndex(++index);
            }
        }
        // 存储分离面面数据
        ProjectSurface surface = ProjectSurface.builder()
                .projectId(param.getProjectId())
                .activeDocId(param.getActiveDocId())
                .surfaceName(param.getSurfaceName())
                .createDt(LocalDateTime.now())
                .surfaceType(param.getSurfaceType())
                .featureId(param.getFeatureId())
                .entityId(param.getEntityId())
                .fromId(param.getFromId())
                .createParam(param.getCreateParam())
                .surfaceIndex(param.getSurfaceIndex())
                .build();
        return mongoTemplate.save(surface);
    }

    /**
     * 查询分离面数据
     * @param projectId
     * @param activeDocId
     * @param assemblyIds
     * @return
     */
    public List<ProjectSurface> getSeparationSurfaces(String projectId, String activeDocId,
                                                      List<String> assemblyIds) {
        List<ProjectSurface> surfaces = mongoTemplate.find
                (Query.query(Criteria.where("projectId").is(projectId).and("activeDocId").is(activeDocId))
                        , ProjectSurface.class);
        List<ProjectSurface> baseSurfaces = surfaces.stream()
                .sorted(Comparator.comparing(ProjectSurface::getSurfaceIndex)).collect(Collectors.toList());
        // 获取装配模式下，实例内的分离面
        if (assemblyIds != null && !assemblyIds.isEmpty()) {
            assemblyIds.forEach(id ->
                    baseSurfaces.addAll(mongoTemplate.find
                            (Query.query(Criteria.where("activeDocId").is(id))
                                    , ProjectSurface.class))
            );
        }
        return baseSurfaces;
    }

    /**
     * 删除指定分离面
     * @param delIds
     */
    public void deleteSeparationSurfaces(List<String> delIds) {
        mongoTemplate.remove(Query.query(Criteria.where("_id").in(delIds)), ProjectSurface.class);
    }
}
